package wolftrace_company.study_android_ui.activity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import wolftrace_company.study_android_ui.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,View.OnLongClickListener{
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button button7;
    private Button button8;
    private Button button9;
    private NotificationManager notificationManager;
    final int noTIFYID = 0x123;
    private String username;
    private String mr = "wolftrace";
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPreferences = getSharedPreferences("wolftrace",MODE_PRIVATE);
         notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        init();
        setListener();
    }
    public void init(){
        button1 = findViewById(R.id.btn_ViewPager);
        button2 = findViewById(R.id.btn_fragment);
        button3 = findViewById(R.id.btn_startPage);
        button4 = findViewById(R.id.toInternet);
        button5 = findViewById(R.id.send_btn);
        button6 = findViewById(R.id.btn_list);
        button7 = findViewById(R.id.btn_dialog);
        button8 = findViewById(R.id.btn_mySelfSpinner);
        button9 = findViewById(R.id.btn_showNotification);
    }
    public void setListener(){
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
        button7.setOnClickListener(this);
        button8.setOnClickListener(this);
        button9.setOnClickListener(this);
        button1.setOnLongClickListener(this);
        button2.setOnLongClickListener(this);
        button3.setOnLongClickListener(this);
        button4.setOnLongClickListener(this);
        button5.setOnLongClickListener(this);
        button6.setOnLongClickListener(this);
        button7.setOnLongClickListener(this);
        button8.setOnLongClickListener(this);
        button9.setOnLongClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_fragment:
                startActivity(new Intent(MainActivity.this, ShowFragActivity.class));
                break;
            case R.id.btn_ViewPager:
                startActivity(new Intent(MainActivity.this,ShowViewPagerActivity.class));
                break;
            case R.id.btn_startPage:
                startActivity(new Intent(MainActivity.this,StartPageActivity.class));
                break;
            case R.id.toInternet:
                startActivity(new Intent(MainActivity.this,GetDataActivity.class));
                break;
            case R.id.send_btn:
                startActivity(new Intent(MainActivity.this,SendDataActicity.class));
                break;
            case R.id.btn_list:
                startActivity(new Intent(MainActivity.this,ListPageActivity.class));
                break;
            case R.id.btn_dialog:
                startActivity(new Intent(MainActivity.this,ShowMyDialogActitvity.class));
                break;
            case R.id.btn_mySelfSpinner:
                startActivity(new Intent(MainActivity.this,MySpinnerActivity.class));
                break;
            case R.id.btn_showNotification:
                Notification.Builder builder = new Notification.Builder(this);
                builder.setAutoCancel(true);
                builder.setSmallIcon(R.drawable.ic_launcher_background);
                builder.setContentTitle("我会来到这里");
                builder.setContentText("点击查看详情");
                builder.setWhen(System.currentTimeMillis());
                Intent intent = new Intent(MainActivity.this,ListPageActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                builder.setContentIntent(pendingIntent);
                notificationManager.notify(noTIFYID,builder.build());


                if(sharedPreferences.getString("username",username)!=null){
                    if (sharedPreferences.getString("username",mr).equals(mr)){
                        Toast.makeText(MainActivity.this,"我想也是最后一次了",Toast.LENGTH_LONG).show();
                        startActivity(new Intent(MainActivity.this,ListPageActivity.class));
                    }
                }else{
                    username = mr;
                    Toast.makeText(MainActivity.this,"第一次吧",Toast.LENGTH_LONG).show();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("username",username);
                    editor.apply();
                }
        }
    }

    public void exit(){
        if(System.currentTimeMillis()-exiTime>2000){
            Toast.makeText(MainActivity.this,"再按一次退出程序",Toast.LENGTH_LONG).show();
            exiTime = System.currentTimeMillis();
        }else{
            finish();
            System.exit(0);
        }
    }
    private long exiTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            exit();
            //hello
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    public boolean onLongClick(View v) {
                //hello
                registerForContextMenu(v);
                System.out.print("长按时间哈哈");
                openContextMenu(v);
        return false;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater menuInflater = new MenuInflater(this);
        menuInflater.inflate(R.menu.mymenuq,menu);
        menu.setHeaderTitle("please choice");
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = new MenuInflater(this);
        menuInflater.inflate(R.menu.mymenu,menu);
        return super.onCreateOptionsMenu(menu);
    }



    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(MainActivity.this,item.getTitle(),Toast.LENGTH_LONG).show();
        return super.onContextItemSelected(item);
    }
}
